<?php

class user_model extends CI_Model
{
    protected $_table="mock_user";
    function login ($user,$pass){
        $this->db->where("user_name",$user);
        $this->db->where("user_password",$pass);
        $query= $this->db->get($this->_table);
        if ($query->num_rows() ==1 ){
            return $query->result();
        }
        else{
            return false;
        }
    }
    function listUser($end,$start){
        return $this->db->get($this->_table)->result_array();
    }
    public function checkName($data){
        $this->db->where('user_name',$data);
        $numrows = $this->db->count_all_results($this->_table);
        if($numrows >0){
            return FALSE;
        }else{
            return TRUE;
        }
    }
    public function checkEmail($data){
        $this->db->where('user_email',$data);
        $numrows = $this->db->count_all_results($this->_table);
        if($numrows >0){
            return FALSE;
        }else{
            return TRUE;
        }
    }
    public function checkUser_update($field,$data,$id=''){
        if($id != ''){
            $this->db->where('user_id !=',$id);
        }
        $this->db->where($field,$data);
        $numrows = $this->db->count_all_results($this->_table);
        if($numrows >0){
            return FALSE;
        }else{
            return TRUE;
        }
    }
    public function checkPass($id){
        if($id != ''){
            $this->db->where('user_id =',$id);
        }
        return $this->db->get($this->_table)->row_array(['user_password']);
    }


//Cuong
    public function totalUsers(){
        return $this->db->count_all($this->_table);
    }
    public function listUsers($off,$start){
        $this->db->limit($off,$start);
        return $this->db->get($this->_table)->result_array();
    }
    public function updateUser($id,$data){
        $this->db->where("user_id",$id);
        $this->db->update($this->_table,$data);
    }
    public function getdata($id){
        $this->db->where("user_id",$id);
        return $this->db->get($this->_table)->row_array();
    }

    public function delete($id){
        $this->db->delete($this->_table,array('user_id'=>$id));
    }

    public function sort($type,$order){
        $this->db->order_by($type,$order);
        return $this->db->get($this->_table);
    }
    public function addUser($data){
        return $this->db->insert($this->_table,$data);
    }
}